package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.VehicleMapper;
import cn.tedu._03vehicle.pojo.dto.VehicleAddParam;
import cn.tedu._03vehicle.pojo.dto.VehicleListQuery;
import cn.tedu._03vehicle.pojo.entiy.User;
import cn.tedu._03vehicle.pojo.entiy.Vehicle;
import cn.tedu._03vehicle.pojo.vo.UserVO;
import cn.tedu._03vehicle.pojo.vo.VehicleListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@Validated
@Tag(name = "02-车辆模块")
@Slf4j
@RestController
@RequestMapping("/v1/vehicle/")
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;

    @Operation(summary = "添加车辆")
    @ApiOperationSupport(order = 10)
    @PostMapping("add")
    public JsonResult add(@RequestBody VehicleAddParam vehicleAddParam, HttpSession session) {
        log.debug("vehicleAddParam = " + vehicleAddParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
        //登录状态校验已从拦截器实现
        //已经登录  调接口插入数据
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam, vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicle.setUserId(userVO.getId());
        log.debug("vehicle = " + vehicle);
        vehicleMapper.insertVehicle(vehicle);
        return JsonResult.ok();
    }

    @Operation(summary = "查询车辆")
    @ApiOperationSupport(order = 20)
    @GetMapping("list")
    @Parameters({
            @Parameter(name = "brand"),
            @Parameter(name = "license"),
            @Parameter(name = "vehicleListQuery", hidden = true)
    })
    public JsonResult list(VehicleListQuery vehicleListQuery, HttpSession session) {
        UserVO userVO = (UserVO) session.getAttribute("user");
        //登录状态校验已从拦截器实现
//        if (userVO == null) {//未登录
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }
        String brand = vehicleListQuery.getBrand();
        String license = vehicleListQuery.getLicense();
        Long userId = userVO.getId();
        List<VehicleListVO> vehicleListVOS =
                vehicleMapper.selectVehicle(brand, license, userId);
        //返回响应
        return JsonResult.ok(vehicleListVOS);
    }

    @Operation(summary = "删除车辆")
    @ApiOperationSupport(order = 30)
    @PostMapping("del/{id}")
    public JsonResult del(@Range(min = 1,max = 500,message = "车辆编号必须在1-500之间")
                          @PathVariable Long id, HttpSession session) {
        if (id<0){
            //手动抛出异常  主要演示:全局异常处理器
            throw new IllegalArgumentException("车辆编号不能小于0");
        }
        UserVO userVO = (UserVO) session.getAttribute("user");
        //登录状态校验已从拦截器实现
//        if (userVO == null) {//未登录
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }
        //已经登录
        Long userId = userVO.getId();
        int i = vehicleMapper.deleteVehicleById(id, userId);
        return JsonResult.ok();
    }


}
